package debut.peinture;

import java.io.IOException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class FragDiapo extends Fragment {
	private View principale = null;
	private boolean changer = true;
	private Timer periodique = null;
	private Thread th = null;
	
	@Override
	public View onCreateView(LayoutInflater inflateur, ViewGroup container, Bundle etatSauve) {
		principale = inflateur.inflate(R.layout.fragmentdiapo, container, false);
		return principale;
	}

	/*
	@Override
    public void onResume() {
		super.onResume();
		transitionTableau();
    }
	
	@Override
    public void onPause() {
		super.onPause();
	}
	*/
	
	public void lanceDiapo() {
		AnimationDrawable ad = new AnimationDrawable();
		periodique = new Timer();
    	periodique.schedule(new changeTableau(), 4000, 4000);
	}
	
	public void arreteDiapo() {
		periodique.cancel();
	}
	
	private class changeTableau extends TimerTask {    	
    	public void run() {
    		handleur.sendEmptyMessage(0);
    	}
    }
	
	private Handler handleur = new Handler() {
		public void handleMessage(Message msg) {
			if(changer) {
				((DroidPaintings)principale.getContext()).indexencours += 1;
				transitionTableau();
			}
		}
	};
	
	public void transitionTableau() {
		final View vtab = principale.findViewById(R.id.tableau);
		if(changer && (((DroidPaintings)principale.getContext()).curseur != null)) {
			changer = false;
			if(th!=null) th.interrupt();
//			((ImageView)vtab).setImageResource(R.drawable.peinture);
			((DroidPaintings)principale.getContext()).curseur.moveToPosition(((DroidPaintings)principale.getContext()).indexencours);
			if( ((DroidPaintings)principale.getContext()).curseur.isAfterLast() ) {
				((DroidPaintings)principale.getContext()).indexencours = 0;
        		((DroidPaintings)principale.getContext()).curseur.moveToFirst();
			}
       		th = new Thread() {
				@Override
				public void run() {
					Bitmap img = null;
					String[] colonnes = principale.getContext().getResources().getStringArray(R.array.colonnes);
					String lien = ((DroidPaintings)principale.getContext()).curseur.getString(
												((DroidPaintings)principale.getContext()).curseur.getColumnIndex(colonnes[0]));
	        		boolean fait = false;
	    			try {
//		    				URL urlimage = new URL(lien);
	    				img = BitmapFactory.decodeStream((new URL(lien)).openConnection().getInputStream());
	    				fait = true;
	    			} catch (IOException e) {
//		    				Log.e(Context.ACTIVITY_SERVICE, "ImageAdapteur:getView:erreur accès image" + e.getMessage());
	    			} catch (OutOfMemoryError e) {
	    				fait = false;
	    			}
	    			if(fait) {
	    				DisplayMetrics metrics = new DisplayMetrics();
	    				((Activity) principale.getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
	    				int largeur = img.getWidth() * metrics.heightPixels / img.getHeight();
	    				int hauteur = metrics.heightPixels;
	    				final Bitmap imgbis = Bitmap.createScaledBitmap(img, largeur, hauteur, false);
	        			((Activity) principale.getContext()).runOnUiThread(new Runnable() {
	        				@Override
							public void run() {
								((ImageView)vtab).setImageBitmap(imgbis);
	    	    				Animation animp = AnimationUtils.loadAnimation(vtab.getContext(), R.anim.rotor);
	    	        			vtab.startAnimation(animp);
	    	        			changer = true;
							}
						});
	    			}
				}
			};
			th.start();
		}
	}

}